home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_016 / source.files / readpict.c < prev    next >
Text File  |  1992-05-06  |  11KB  |  290 lines

  1. /** ReadPict.c **************************************************************
  2.  *
  3.  * Read an ILBM raster image file.                   23-Jan-86.
  4.  *
  5.  * By Jerry Morrison, Steve Shaw, and Steve Hayes, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * USE THIS AS AN EXAMPLE PROGRAM FOR AN IFF READER.
  9.  *
  10.  * The IFF reader portion is essentially a recursive-descent parser.
  11.  ****************************************************************************/
  12.  
  13. #define LOCAL    static
  14.  
  15. #include "iff/intuall.h"
  16. #include "libraries/dos.h"
  17. #include "libraries/dosextens.h"
  18. #include "iff/ilbm.h"
  19. #include "iff/readpict.h"
  20.  
  21. /* This example's max number of planes in a bitmap. Could use MaxAmDepth. */
  22. #define EXDepth 5
  23. #define maxColorReg (1<<EXDepth)
  24. #define MIN(a,b) ((a)<(b)?(a):(b))
  25.  
  26. #define SafeFreeMem(p,q) {if(p)FreeMem(p,q);}
  27.  
  28. /* Define the size of a temporary buffer used in unscrambling the ILBM rows.*/
  29. #define bufSz 512
  30.  
  31. /*------------ ILBM reader -----------------------------------------------*/
  32. /* ILBMFrame is our "client frame" for reading FORMs ILBM in an IFF file.
  33.  * We allocate one of these on the stack for every LIST or FORM encountered
  34.  * in the file and use it to hold BMHD & CMAP properties. We also allocate
  35.  * an initial one for the whole file.
  36.  * We allocate a new GroupContext (and initialize it by OpenRIFF or
  37.  * OpenRGroup) for every group (FORM, CAT, LIST, or PROP) encountered. It's
  38.  * just a context for reading (nested) chunks.
  39.  *
  40.  * If we were to scan the entire example file outlined below:
  41.  *    reading          proc(s)                new               new
  42.  *
  43.  * --whole file--   ReadPicture+ReadIFF   GroupContext        ILBMFrame
  44.  * CAT              ReadICat                GroupContext
  45.  *   LIST           GetLiILBM+ReadIList       GroupContext        ILBMFrame
  46.  *     PROP ILBM    GetPrILBM                   GroupContext
  47.  *       CMAP       GetCMAP
  48.  *       BMHD       GetBMHD
  49.  *     FORM ILBM    GetFoILBM                   GroupContext        ILBMFrame
  50.  *       BODY       GetBODY
  51.  *     FORM ILBM    GetFoILBM                   GroupContext        ILBMFrame
  52.  *       BODY       GetBODY
  53.  *   FORM ILBM      GetFoILBM                 GroupContext        ILBMFrame
  54.  */
  55.  
  56. /* NOTE: For a small version of this program, set Fancy to 0.
  57.  * That'll compile a program that reads a single FORM ILBM in a file, which
  58.  * is what DeluxePaint produces. It'll skip all LISTs and PROPs in the input
  59.  * file. It will, however, look inside a CAT for a FORM ILBM.
  60.  * That's suitable for 90% of the uses.
  61.  *
  62.  * For a fancier version that handles LISTs and PROPs, set Fancy to 1.
  63.  * That'll compile a program that dives into a LIST, if present, to read
  64.  * the first FORM ILBM. E.g. a DeluxePrint library of images is a LIST of
  65.  * FORMs ILBM.
  66.  *
  67.  * For an even fancier version, set Fancy to 2. That'll compile a program
  68.  * that dives into non-ILBM FORMs, if present, looking for a nested FORM ILBM.
  69.  * E.g. a DeluxeVideo C.S. animated object file is a FORM ANBM containing a
  70.  * FORM ILBM for each image frame. */
  71. #define Fancy  0
  72.  
  73. /* Global access to client-provided pointers.*/
  74. LOCAL Allocator *gAllocator = NULL;
  75. LOCAL struct BitMap *gBM = NULL;    /* client's bitmap.*/
  76. LOCAL ILBMFrame *giFrame = NULL;    /* "client frame".*/
  77.  
  78. /** GetFoILBM() *************************************************************
  79.  *
  80.  * Called via ReadPicture to hanle every FORM encountered in an IFF file.
  81.  * Reads FORMs ILBM and skips all others.
  82.  * Inside a FORM ILBM, it stops once it reads a BODY. It complains if it
  83.  * finds no BODY or if it has no BMHD to decode the BODY.
  84.  *
  85.  * Once we find a BODY chunk, we'll allocate the BitMap and read the image.
  86.  *
  87.  ****************************************************************************/
  88. LOCAL BYTE bodyBuffer[bufSz];
  89. IFFP GetFoILBM(parent)  GroupContext *parent;  {
  90.    /*compilerBug register*/ IFFP iffp;
  91.    GroupContext formContext;
  92.    ILBMFrame ilbmFrame;        /* only used for non-clientFrame fields.*/
  93.    register int i;
  94.    LONG plsize;    /* Plane size in bytes. */
  95.    int nPlanes; /* number of planes in our display image */
  96.  
  97.     /* Handle a non-ILBM FORM. */
  98.     if (parent->subtype != ID_ILBM) {
  99. #if Fancy >= 2
  100.     /* Open a non-ILBM FORM and recursively scan it for ILBMs.*/
  101.     iffp = OpenRGroup(parent, &formContext);
  102.     CheckIFFP();
  103.     do {
  104.         iffp = GetF1ChunkHdr(&formContext);
  105.         } while (iffp >= IFF_OKAY);
  106.     if (iffp == END_MARK)
  107.         iffp = IFF_OKAY;    /* then continue scanning the file */
  108.     CloseRGroup(&formContext);
  109.     return(iffp);
  110. #else
  111.         return(IFF_OKAY); /* Just skip this FORM and keep scanning the file.*/
  112. #endif
  113.     }
  114.  
  115.    ilbmFrame = *(ILBMFrame *)parent->clientFrame;
  116.    iffp = OpenRGroup(parent, &formContext);
  117.    CheckIFFP();
  118.  
  119.    do switch (iffp = GetFChunkHdr(&formContext)) {
  120.       case ID_BMHD: {
  121.     ilbmFrame.foundBMHD = TRUE;
  122.     iffp = GetBMHD(&formContext, &ilbmFrame.bmHdr);
  123.     break; }
  124.       case ID_CMAP: {
  125.     ilbmFrame.nColorRegs = maxColorReg;  /* we have room for this many */
  126.     iffp = GetCMAP(
  127.        &formContext, (WORD *)&ilbmFrame.colorMap[0], &ilbmFrame.nColorRegs);
  128.             /* was &ilbmFrame.colorMap, (fixed) robp. */
  129.     break; }
  130.       case ID_BODY: {
  131.          if (!ilbmFrame.foundBMHD)  return(BAD_FORM);    /* No BMHD chunk! */
  132.  
  133.      nPlanes = MIN(ilbmFrame.bmHdr.nPlanes, EXDepth);
  134.      InitBitMap(
  135.         gBM,
  136.         nPlanes,
  137.         ilbmFrame.bmHdr.w,
  138.         ilbmFrame.bmHdr.h);
  139.      plsize = RowBytes(ilbmFrame.bmHdr.w) * ilbmFrame.bmHdr.h;
  140.      /* Allocate all planes contiguously.  Not really necessary,
  141.       * but it avoids writing code to back-out if only enough memory
  142.       * for some of the planes.
  143.       * WARNING: Don't change this without changing the code that
  144.       * Frees these planes.
  145.       */
  146.      if (gBM->Planes[0] =
  147.         (PLANEPTR)(*gAllocator)(nPlanes * plsize))
  148.         {
  149.         for (i = 1; i < nPlanes; i++)
  150.         gBM->Planes[i] = (PLANEPTR) gBM->Planes[0] + plsize*i;
  151.         iffp = GetBODY(
  152.         &formContext,
  153.         gBM,
  154.         NULL,
  155.         &ilbmFrame.bmHdr,
  156.         bodyBuffer,
  157.         bufSz);
  158.         if (iffp == IFF_OKAY) iffp = IFF_DONE;    /* Eureka */
  159.         *giFrame = ilbmFrame;  /* Copy fields to client's frame.*/
  160.         }
  161.      else 
  162.         iffp = CLIENT_ERROR;    /* not enough RAM for the bitmap */
  163.          break; }
  164.       case END_MARK: { iffp = BAD_FORM; break; } /* No BODY chunk! */
  165.       } while (iffp >= IFF_OKAY);  /* loop if valid ID of ignored chunk or a
  166.               * subroutine returned IFF_OKAY (no errors).*/
  167.  
  168.    if (iffp != IFF_DONE)  return(iffp);
  169.  
  170.    /* If we get this far, there were no errors. */
  171.    CloseRGroup(&formContext);
  172.    return(iffp);
  173.    }
  174.  
  175. /** Notes on extending GetFoILBM ********************************************
  176.  *
  177.  * To read more kinds of chunks, just add clauses to the switch statement.
  178.  * To read more kinds of property chunks (GRAB, CAMG, etc.) add clauses to
  179.  * the switch statement in GetPrILBM, too.
  180.  *
  181.  * To read a FORM type that contains a variable number of data chunks--e.g.
  182.  * a FORM FTXT with any number of CHRS chunks--replace the ID_BODY case with
  183.  * an ID_CHRS case that doesn't set iffp = IFF_DONE, and make the END_MARK
  184.  * case do whatever cleanup you need.
  185.  *
  186.  ****************************************************************************/
  187.  
  188. /** GetPrILBM() *************************************************************
  189.  *
  190.  * Called via ReadPicture to handle every PROP encountered in an IFF file.
  191.  * Reads PROPs ILBM and skips all others.
  192.  *
  193.  ****************************************************************************/
  194. #if Fancy
  195. IFFP GetPrILBM(parent)  GroupContext *parent;  {
  196.    /*compilerBug register*/ IFFP iffp;
  197.    GroupContext propContext;
  198.    ILBMFrame *ilbmFrame = (ILBMFrame *)parent->clientFrame;
  199.  
  200.    if (parent->subtype != ID_ILBM)
  201.       return(IFF_OKAY);    /* just continue scaning the file */
  202.  
  203.    iffp = OpenRGroup(parent, &propContext);
  204.    CheckIFFP();
  205.  
  206.    do switch (iffp = GetPChunkHdr(&propContext)) {
  207.       case ID_BMHD: {
  208.     ilbmFrame->foundBMHD = TRUE;
  209.     iffp = GetBMHD(&propContext, &ilbmFrame->bmHdr);
  210.     break; }
  211.       case ID_CMAP: {
  212.     ilbmFrame->nColorRegs = maxColorReg; /* we have room for this many */
  213.     iffp = GetCMAP(
  214.       &propContext, (WORD *)&ilbmFrame->colorMap, &ilbmFrame->nColorRegs);
  215.     break; }
  216.       } while (iffp >= IFF_OKAY);  /* loop if valid ID of ignored chunk or a
  217.               * subroutine returned IFF_OKAY (no errors).*/
  218.  
  219.    CloseRGroup(&propContext);
  220.    return(iffp == END_MARK ? IFF_OKAY : iffp);
  221.    }
  222. #endif
  223.  
  224. /** GetLiILBM() *************************************************************
  225.  *
  226.  * Called via ReadPicture to handle every LIST encountered in an IFF file.
  227.  *
  228.  ****************************************************************************/
  229. #if Fancy
  230. IFFP GetLiILBM(parent)  GroupContext *parent;  {
  231.     ILBMFrame newFrame;    /* allocate a new Frame */
  232.  
  233.     newFrame = *(ILBMFrame *)parent->clientFrame;  /* copy parent frame */
  234.  
  235.     return( ReadIList(parent, (ClientFrame *)&newFrame) );
  236.     }
  237. #endif
  238.  
  239. /** ReadPicture() **********************************************************/
  240. IFFP ReadPicture(file, bm, iFrame, allocator)
  241.    LONG file;
  242.    struct BitMap *bm;
  243.    ILBMFrame *iFrame;    /* Top level "client frame".*/
  244.  
  245.     /* **** ERROR IN SOURCE CODE, WAS jFrame, now iFrame */
  246.     /* fixed */
  247.  
  248.    Allocator *allocator;
  249.    {
  250.    IFFP iffp = IFF_OKAY;
  251.  
  252. #if Fancy
  253.    iFrame->clientFrame.getList = GetLiILBM;
  254.    iFrame->clientFrame.getProp = GetPrILBM;
  255. #else
  256.    iFrame->clientFrame.getList = SkipGroup;
  257.    iFrame->clientFrame.getProp = SkipGroup;
  258. #endif
  259.    iFrame->clientFrame.getForm = GetFoILBM;
  260.    iFrame->clientFrame.getCat  = ReadICat ;
  261.  
  262.    /* Initialize the top-level client frame's property settings to the
  263.     * program-wide defaults. This example just records that we haven't read
  264.     * any BMHD property or CMAP color registers yet. For the color map, that
  265.     * means the default is to leave the machine's color registers alone.
  266.     * If you want to read a property like GRAB, init it here to (0, 0). */
  267.    iFrame->foundBMHD  = FALSE;
  268.    iFrame->nColorRegs = 0;
  269.  
  270.    gAllocator = allocator;
  271.    gBM = bm;
  272.    giFrame = iFrame;
  273.   /* Store a pointer to the client's frame in a global variable so that
  274.    * GetFoILBM can update client's frame when done.  Why do we have so
  275.    * many frames & frame pointers floating around causing confusion?
  276.    * Because IFF supports PROPs which apply to all FORMs in a LIST,
  277.    * unless a given FORM overrides some property.  
  278.    * When you write code to read several FORMs,
  279.    * it is ssential to maintain a frame at each level of the syntax
  280.    * so that the properties for the LIST don't get overwritten by any
  281.    * properties specified by individual FORMs.
  282.    * We decided it was best to put that complexity into this one-FORM example,
  283.    * so that those who need it later will have a useful starting place.
  284.    */
  285.  
  286.    iffp = ReadIFF(file, (ClientFrame *)iFrame);
  287.    return(iffp);
  288.    }
  289.  
  290.